<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class RoomController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Room");

	}
	
	function index(){
		
		
	//	dump(F("Country"));

		$search=false;

		if(I("get.building")){

			$map['building']=I("get.building");

			$search=true;
		}

		if(I("get.type")){

			$map['type']=I("get.type");

			$search=true;
		}


		if(I("get.status")!=''){
			$map['status']=I("get.status");

			$search=true;

		}





		if(I("get.keyword")){

			$map['name']=array("like","%".trim(I("get.keyword"))."%");
			$search=true;

		}

		$this->assign("search",$search);

		$count=$this->db->where($map)->count();
		$page = $this->page($count);
		$list=$this->db->limit($page->firstRow . ',' . $page->listRows)->order ( "building asc , floor asc, name asc")->where($map)->select();
		
		$this->assign("Page",$page->show(""));
		
		$this->assign("list",$list);
		
		$this->display();
	}
	

	
	
	function add(){

		if(IS_POST){
			cookie("add_var_building",$_POST[building]);
			cookie("add_var_floor",$_POST[floor]);

			if ($this->db->create($_POST)) {
				if ($this->db->add()!==false) {

					echo "添加成功";

//					$this->success("添加成功！", U("index"));
				} else {
//					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{

			$data["building"]=cookie("add_var_building");
			$data["floor"]=cookie("add_var_floor");

			$this->assign("data",$data);

			$this->display("form");
		}
		}

	
	function edit(){
		
		
		if(IS_POST){
			if ($this->db->create($_POST)) {
				
				if ($this->db->save()!==false) {

					$this->db->data_cache();
					$this->success("保存成功！");
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}

		$r=$this->db->where("id=$id")->find();
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->display("form");
		}
	}
	

	
	
	}